import unittest
from handler.Estim import Estim
import time
from unittest.mock import patch


class test_Estim(unittest.TestCase):
	def setUp(self):
		self._estim = None

	def tearDown(self):
		if self._estim is not None:
			self._estim.close()

	@patch('handler.Estim.estim2b')
	def test_set_output_will_be_called(self, mock_estim2b):
		self._estim = self._arrange_estim()
		self._estim.change_state(None, 'a_on_b_off')
		time.sleep(1)

		self.assertGreater(self._estim._estim.setOutput.call_count, 0)

	def _arrange_estim(self):
		content = {
			'name': 'test',
			'handler': 'estim',
			'port': 'COM8',
			'sleep': 0.2,
			'options': {
				'a_on_b_off': {
					'a': '(3 * $seconds_a + 100) * ($seconds_a < 5)',
				},
			},
		}

		estim = Estim(content)

		return estim
